package tongwentongshu.com.app.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.OrderEvent;
import tongwentongshu.com.app.contract.RegisterContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.presenter.RegisterPresenter;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/11.
 */
public class RegisterActivity extends BaseActivity implements RegisterContract.View, View.OnClickListener {
    @BindView(R.id.ll_register)
    LinearLayout mainView;
    private String RegisterStatus;
    private EditText etNickname, etPhone, etCode, etPw, etRpw;
    private Button next;
    private TextView info, sendCode;
    private String phone, nickname;
    private RegisterPresenter registerPresenter;

    @Override
    public String getRegisterStatus() {
        return RegisterStatus;
    }

    @Override
    public String getPhone() {
        return etPhone != null ? etPhone.getText().toString().trim() : phone;
    }

    @Override
    public String getNickname() {
        return etNickname != null ? etNickname.getText().toString().trim() : nickname;
    }

    @Override
    public String getCode() {
        return etCode.getText().toString().trim();
    }

    @Override
    public String getPw() {
        return etPw.getText().toString().trim();
    }

    @Override
    public String getRpw() {
        return etRpw.getText().toString().trim();
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_register;
    }

    @Override
    protected int getTitleId() {
        return R.string.register;
    }

    @Override
    protected void initView() {
        RegisterStatus = getIntent().getExtras().getString(Cache.REGISTER_KEY);
        if (RegisterStatus.equals(Cache.REGISTER_VERIFICATION)) {//手机号验证布局
            View view = getLayoutInflater().inflate(R.layout.register_user_name, (ViewGroup) mainView.getParent(), false);
            etNickname = (EditText) view.findViewById(R.id.et_login_nickname);
            etPhone = (EditText) view.findViewById(R.id.et_login_phone);
            mainView.addView(view);
        } else if (RegisterStatus.equals(Cache.REGISTER_PASSWORD)) {//注册布局
            phone = getIntent().getExtras().getString("phone");
            nickname = getIntent().getExtras().getString("nickname");
            View view = getLayoutInflater().inflate(R.layout.register_verification, (ViewGroup) mainView.getParent(), false);
            etCode = (EditText) view.findViewById(R.id.et_login_code);
            etCode.setInputType(InputType.TYPE_CLASS_NUMBER
                    | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            sendCode = (TextView) view.findViewById(R.id.btn_send_code);
            sendCode.setOnClickListener(this);
            info = (TextView) view.findViewById(R.id.tv_info);
            info.setText("验证码已发到手机号 " + phone);
            etPw = (EditText) view.findViewById(R.id.et_login_password);
            etRpw = (EditText) view.findViewById(R.id.et_login_again_password);
            mainView.addView(view);
            timer.start();
        }
        next = (Button) findViewById(R.id.btn_next);
        next.setOnClickListener(this);
    }
    public void onEventMainThread(OrderEvent result) {
        finish();
    }

    @Override
    protected void initData() {
        registerPresenter = new RegisterPresenter(this);
    }

    private boolean codeMill = false;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                if (RegisterStatus.equals(Cache.REGISTER_VERIFICATION)) {
                    registerPresenter.sendCode();
                } else if (RegisterStatus.equals(Cache.REGISTER_PASSWORD)) {
                    registerPresenter.register();
                }
                break;
            case R.id.btn_send_code:
                codeMill = true;
                registerPresenter.sendCode();
                break;
            default:
                break;
        }
    }

    private void toNext() {
        Bundle b = new Bundle();
        b.putString(Cache.REGISTER_KEY, Cache.REGISTER_PASSWORD);
        b.putString("phone", etPhone.getText().toString().trim());
        b.putString("nickname", etNickname.getText().toString().trim());
        startActivity(RegisterActivity.class, b);
    }

    @Override
    public void onSuccess(String msg) {
        ToastUtil.show(mContext, msg);
        if (RegisterStatus.equals(Cache.REGISTER_VERIFICATION)) {
            toNext();
        } else if (RegisterStatus.equals(Cache.REGISTER_PASSWORD)) {
            if (codeMill) {
                timer.start();
                codeMill = false;
            } else
                EventBus.getDefault().post(new OrderEvent());
        }

    }

    @Override
    public void onShowError(String msg) {
        ToastUtil.show(mContext, msg);
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            sendCode.setText((millisUntilFinished / 1000) + "秒后可重发");
            sendCode.setBackgroundResource(R.color.title_DDD);
            sendCode.setClickable(false);
        }

        @Override
        public void onFinish() {
            sendCode.setEnabled(true);
            sendCode.setBackgroundResource(R.color.title_orange);
            sendCode.setClickable(true);
            sendCode.setText("获取验证码");

        }
    };
}
